<template>
  <div class="edit-container">
    <!-- 导航条 -->
    <van-nav-bar @click-left="toMy" left-arrow title="编辑资料"></van-nav-bar>
    <!-- 头像部分 -->
    <div class="avatar">
      <!-- 头像 -->
      <van-image fit="cover" round :src="userInfo.photo" />
      <!-- 上传组件 -->
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 信息展示 -->
    <van-cell-group>
      <van-cell
        is-link
        title="名称"
        :value="userInfo.name"
        @click="shownameto"
      />
      <van-cell
        is-link
        title="性别"
        :value="userInfo.gender === 0 ? '男' : '女'"
        @click="showGender = true"
      />
      <van-cell
        is-link
        title="生日"
        @click="showBirthdayPop"
        :value="userInfo.birthday"
      />
    </van-cell-group>
    <van-dialog
      v-model="showName"
      @confirm="savename"
      title="修改昵称"
      show-cancel-button
    >
      <van-field ref="namefield" type="text" v-model="name"></van-field>
    </van-dialog>
    <!-- 性别弹框 -->
    <van-popup v-model="showGender" position="bottom">
      <!-- 顶部导航栏 -->
      <van-nav-bar
        title="修改性别"
        left-text="取消"
        @click-left="showGender = false"
      />
      <!-- 性别选择区域 -->
      <van-cell-group>
        <van-cell title="男" is-link @click="changeGender(0)" />
        <van-cell title="女" is-link @click="changeGender(1)" />
      </van-cell-group>
    </van-popup>
    <!-- 生日的编写 -->
    <van-popup position="bottom" v-model="showBirthday">
      <van-datetime-picker
        @confirm="confirmDate"
        @cancel="cancelDate"
        v-model="birthday"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
      />
    </van-popup>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { editUserInfo, uploadPhoto } from '../../api/edit'
// 导入monent.js
import moment from 'moment'
export default {
  name: 'edit',
  data () {
    return {
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      birthday: new Date(2021, 5, 6),
      showName: false,
      name: '',
      showGender: false,
      showBirthday: false // 是否显示生日弹框
    }
  },
  methods: {
    // 上传formdata文件
    afterRead (file) {
      // 实例化formData
      const data = new FormData()
      data.append('photo', file.file)
      uploadPhoto(data).then(res => {
        // console.log('res:', res)
        this.$toast.success('更新成功')
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          photo: res.data.data.photo
        })
      })
    },
    // 修改生日
    confirmDate () {
      console.log('confirmDate')

      const birthday = moment(this.birthday).format('YYYY-MM-DD')
      // console.log('birthday:', birthday)
      editUserInfo({ birthday }).then(res => {
        // console.log('res:', res)
        this.$toast.success('更新成功')
        this.showBirthday = false
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          birthday: res.data.data.birthday
        })
      })
    },
    cancelDate () {
      // console.log('cancelDate')
      this.showBirthday = false
    },
    // 弹出生日
    showBirthdayPop () {
      this.showBirthday = true
      // 设置用户的生日为默认值
      this.birthday = new Date(this.userInfo.birthday)
    },
    // 性别
    changeGender (gender) {
      // console.log('gender:', gender)
      editUserInfo({ gender }).then(res => {
        // console.log('res:', res)
        this.$toast.success('更新成功！')
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          gender
        })
        this.showGender = false
      })
    },
    savename () {
      // console.log('savename')
      editUserInfo({ name: this.name }).then(res => {
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          name: res.data.data.name
        })
      })
    },
    // 性别点击
    // sexname () {},
    shownameto () {
      this.showName = true
      this.name = this.userInfo.name
      this.$nextTick(() => {
        this.$refs.namefield.focus()
      })
    },
    toMy () {
      this.$router.push({ path: '/my' })
    }
  },
  computed: mapState(['userInfo'])
  // computed: {
  //   userInfo () {
  //     return this.$store.state.userInfo
  //   }
  // }
}
</script>

<style lang="less">
.edit-container {
  // 导航条部分
  .van-nav-bar {
    background-color: #3196fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  // 头像部分
  .avatar {
    padding: 20px 0;
    text-align: center;
    position: relative;
    .van-image {
      width: 120px;
      height: 120px;
    }
  }
  .van-uploader {
    // 上传组件
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    height: 120px;
    width: 120px;
    opacity: 0;
    .van-uploader__upload {
      width: 120px;
      height: 120px;
    }
  }
}
</style>
